package tencent_game_server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import tencent_game_server.GameServer.Status;



public class GameRoom extends Thread {
	enum Operation{
		none,
		scissor,
		paper,
		rock
	}
	String roomName="";
	HashMap<String,Operation> userList=new HashMap<>();
	Object lock=new Object();
	static final int maxTime=7;
	AtomicInteger spareTime=new AtomicInteger(0);
	int readyUser;
	IServer server;
	public GameRoom(String firstUser,String room ,IServer server) throws MalformedURLException, RemoteException, NotBoundException{
		super();
		this.roomName=room;
		this.server=server;
		userList.put(firstUser,Operation.none);
	}
	public void addUser(String uname){
		synchronized (lock) {
		userList.put(uname,Operation.none);
		}
	}
	public boolean request(String uname,String op) {
		synchronized (lock) {
			if(userList.get(uname)!=Operation.none)
				return false;
			readyUser++;
			switch (op) {
			case "scissor":userList.replace(uname, Operation.scissor);break;
			case "paper":userList.replace(uname, Operation.paper);break;
			case "rock":userList.replace(uname, Operation.rock);break;
			default:
				break;
			}
			return true;
		}
	}
	public void quite(String user){
		synchronized (lock) {
			Operation status=userList.get(user);
			if(status==null){
				System.out.println("del a unexisting user");
			}
			userList.remove(user);
		}
	}
	public int  userNum() {
		synchronized (lock) {
			return userList.size();
		}
	}
	private void calScore() {
		for(String uname:userList.keySet())
		userList.replace(uname, Operation.none);
	}
	private void calc() throws RemoteException{
		synchronized (lock) {
			boolean s = false,p = false,r = false;
			ArrayList<String> list=new ArrayList<>();
			for(String uname:userList.keySet()){
				Operation op=userList.get(uname);
				switch (op) {
					case none:continue;
					case scissor:s=true;break;
					case paper:p=true;break;
					case rock:r=true;break;
				}
				
				list.add(uname);
			}
			String temp[]=list.toArray(new String[]{});
			if(s&&p&&!r){
				for(String uname:userList.keySet()){
					Operation op=userList.get(uname);
					switch (op) {
						case none:continue;
						case scissor:GameServer.changeScore(uname, 1);break;
						case paper:GameServer.changeScore(uname, -1);break;
						case rock:break;
					}
					userList.replace(uname, Operation.none);
				}
				server.broadCast(temp, "last winner is scissor");
			}else if(s&&!p&&r){
				for(String uname:userList.keySet()){
					Operation op=userList.get(uname);
					switch (op) {
						case none:continue;
						case scissor:GameServer.changeScore(uname, -1);break;
						case paper:break;
						case rock:GameServer.changeScore(uname, 1);break;
					}
					userList.replace(uname, Operation.none);
				}
				server.broadCast(temp, "last winner is rock");
			}else if(!s&&p&&r){
				for(String uname:userList.keySet()){
					Operation op=userList.get(uname);
					switch (op) {
						case none:continue;
						case scissor:break;
						case paper:GameServer.changeScore(uname, 1);break;
						case rock:GameServer.changeScore(uname, -1);break;
					}
					userList.replace(uname, Operation.none);
				}
				server.broadCast(temp, "last winner is paper");
			}else {
				for(String uname:userList.keySet())
					userList.replace(uname, Operation.none);
				server.broadCast(temp, "last result is peace");
			}
			
		}
	}
	public void run() {
		try {
			System.out.println("start a room");
			while(userList.size()>=1){
				synchronized (lock) {
					spareTime.set(maxTime);;
					readyUser=0;
				}
				while(readyUser<2){
					TimeUnit.SECONDS.sleep(2);
				}
				while(spareTime.get()>0){
					spareTime.decrementAndGet();
					TimeUnit.SECONDS.sleep(1);
				}
				calc();
				
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}
}
